import Store from '@/utils/store'
import { history } from 'umi'
import { TokenKey, removeToken, getToken } from '@/utils/auth'

export default {
  namespace: 'user',
  state: {
    userInfo: {
      store_name: Store.fetchLocal('loginInfoSMP') ? Store.fetchLocal('loginInfoSMP').store_name : '',
      headimgurl: Store.fetchLocal('loginInfoSMP') ? Store.fetchLocal('loginInfoSMP').headimgurl : ''
    }
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    // 检查token
    *checkToken() {
      return getToken()
    },
    // 更新用户信息
    *updateUserInfo({ payload }, { put, call }) {
      yield put({
        type: 'save',
        payload: { userInfo: payload }
      })
    },
    // 登出
    *logOut() {
      yield removeToken()
      history.push('/login')
    }
  }
}
